
version 16.0
clear all
set more off

capture log close
log using "master_replicate_log.txt", replace text

* ============================================================
* master_replicate.do — Full pipeline (build panel -> run models)
*   Prereqs (same folder):
*     - build_panel.do
*     - run_from_panel.do   (analysis)
*     - CNRDS_green_patents.xlsx
*     - CSMAR_finance_data.xlsx
*     - GovWorkReports_count_summary..xlsx
* Produces:
*     - data.xlsx / panel_planA.csv (from build)
*     - stata_results.rtf (from analysis)
*     - build_panel_log.txt, replication_log.txt, master_replicate_log.txt
* ============================================================

* Step A: Build panel from raw (idempotent)
capture noisily do build_panel.do

* Sanity checks on the built panel
capture confirm file "data.xlsx"
if _rc {
    di as error "data.xlsx was not created by build_panel.do"
    exit 601
}

import excel using "data.xlsx", firstrow clear
capture confirm variable stkcd
if _rc {
    di as error "data.xlsx missing firm code 'stkcd'"
    exit 602
}
capture confirm variable year
if _rc {
    di as error "data.xlsx missing 'year'"
    exit 603
}
capture confirm variable GI ESGP Slack MEA
if _rc {
    di as error "data.xlsx missing core variables (GI/ESGP/Slack/MEA)"
    exit 604
}
summ year, meanonly
if (r(min) < 2010 | r(max) > 2030) {
    di as error "Year range looks abnormal: " r(min) " - " r(max)
}
count
di as txt "Rows in data.xlsx: " r(N)

* Step B: Run analysis (idempotent)
clear all
capture noisily do run_from_panel.do

* Validate that results table exists
capture confirm file "stata_results.rtf"
if _rc di as error "stata_results.rtf not found after analysis run."

log close
di as result "Done. Full pipeline completed. See: stata_results.rtf"
